*****************************************
* Do-File for the replication of "The Impact of Local Social Policy on the Political Discontent 
* of Economically Deprived Individuals – Evidence from Dutch Communities"
* Authors: Teresa Hummler & Paul Vierus
*****************************************

* The analyses is based on the NELLS dataset including the geolocation of each respondent. 

* Pleas see this website for instructions to get the geocoded dataset. 
* https://www.nells.nl/

*****************************************
* Notes: This do-file needs two ados. 
* Make sure they are installed:
ssc install fre, replace
ssc install rev, replace

version 17.0 //Stata Version 17.0 was used
*****************************************

//Changing working directory
cd "C:\Users\vierus\OneDrive - Universität Duisburg-Essen\Teresa und Paul\HV_Dissatisfaction\Analyse\Replikation\_test"
	
//loading dataset
use "NELLS_W1_W2_with_geolocation.dta", clear
			
//drop cases who moved between wave 1 und wave 2
drop if w2cmovedsr==1 			
drop if w2cmovedadm == 1
drop if w1cmun2009id!=w2cmun2012id

//balancing the panel
drop if w2cpanel==0

//transforming dataset to long
preserve
	keep w1* gm_code GM_NAAM_muni GM_CODE_dest GM_NAAM
		save "nells_wave1.dta", replace			
restore

preserve 
	keep w2* gm_code GM_NAAM_muni GM_CODE_dest GM_NAAM
	keep if w2cpanel == 1
		save "nells_wave2.dta", replace		
restore

use "nells_wave1.dta", clear 
append using "nells_wave2.dta", gen(wave2)


//creating identifier for persons and waves
gen double id = . 
format id %15.0f

replace id = w1crespnr if w1crespnr != .
	assert w1crespnr == id if id != .
replace id = w2crespnr  if w2crespnr != . & id==.

sort id w1crespnr w2crespnr
bys id: gen n_wave = _N
bys id: gen wave = _n

//keep persons only with two waves
sort id n_wave
keep if n_wave == 2

*saving working dataset
save "nells_panel_long_cleaned.dta", replace
	
	
**# Merging & preparing contextual data
*******************************************************************
use "nells_panel_long_cleaned.dta", clear

//dropping respondents without geolocated information
drop if GM_NAAM_muni == ""		

//harmonization of municipalities, which have been merged
replace GM_NAAM_muni = "Den Haag" if GM_NAAM_muni == "'s-Gravenhage"	
replace GM_NAAM_muni = "Venlo" if GM_NAAM_muni == "Arcen en Velden"		
replace GM_NAAM_muni = "Waadhoeke" if GM_NAAM_muni == "Franekeradeel"	
replace GM_NAAM_muni = "Berg en Dal" if GM_NAAM_muni == "Millingen aan de Rijn"	
replace GM_NAAM_muni = "Oldambt" if GM_NAAM_muni == "Scheemda"		
replace GM_NAAM_muni = "Krimpenerwaard" if GM_NAAM_muni == "Vlist"			
replace GM_NAAM_muni = "Oldambt" if GM_NAAM_muni == "Winschoten"			
replace GM_NAAM_muni = "Westerkwartier" if GM_NAAM_muni == "Zuidhorn"			
replace GM_NAAM_muni = "Krimpenerwaard" if GM_NAAM_muni == "Schoonhoven"			
replace GM_NAAM_muni = "Dijk en Waard" if GM_NAAM_muni == "Heerhugowaard"		

//merge social expenditure and degree of urbanization
merge m:1 GM_NAAM_muni using "HV_Dissatisfaction_Social_Expenditure" , keep(1 3) nogen
merge m:1 gm_code using "HV_Dissatisfaction_Degree_Urbanization.dta", keep(1 3)  nogen 
merge m:1 GM_NAAM_muni using "HV_Dissatisfaction_HH_Min.dta", keep(1 3)  nogen 
merge m:1 gm_code using "HV_Dissatisfaction_Social_Expenditure_2.dta", keep(1 3)  nogen 	

//transforming in the right dataformat
foreach X in 	government security infrastructure economy education sport social health housing	{
gen m_`X' = . 
replace m_`X' = `X'_2010 if wave == 1
replace m_`X' = `X'_2014 if wave == 2	
}		
	
//dropping the wide variables
drop government_2010 government_2014 security_2010 security_2014 infrastructure_2010 infrastructure_2014 economy_2010 economy_2014 education_2010 education_2014 sport_2010 sport_2014 social_2010 social_2014 health_2010 health_2014 housing_2010 housing_2014 

//preparing variable capturing degree of urbanisation
gen m_sted = . 
	replace m_sted = STED_2009 if wave == 1
	replace m_sted = STED_2012 if wave == 2
	lab var m_sted "Degree of Urbanisation 1-5 (1 = Village; 5 = City)"

rev m_sted, replace		//reverse coding
drop STED_2009 STED_2012

//preparing variable capturing share of household at minimum
gen hh_socmin = . 
	replace hh_socmin = P_SOCMINH_2009 if wave == 1
	replace hh_socmin = P_SOCMINH_2012 if wave == 2
	lab var hh_socmin "Share of HH in social minimum"
	
drop P_SOCMINH_2009 P_SOCMINH_2012	

**# Merging & prepare individual data
*******************************************************************

//political distrust
alpha w1sce11a w1sce11b  , item gen(distrust2_w1)
alpha w2sce11a w2sce11b  , item gen(distrust2_w2)

gen distrust2 = distrust2_w1
replace distrust2 = distrust2_w2 if distrust2 == .
lab var distrust2 "Political distrust"	
						
//political dissatisfaction
gen dissatis = w1sce3e if wave == 1
replace dissatis = w2sce3e if wave == 2
lab var dissatis "Political dissatisfaction"
lab define dissatis 1 "strongly agree" 5 "strongly disagree"
lab val dissatis dissatis		
rev dissatis, replace

rename w2sce3e dissatis_w2
rev dissatis_w2, replace
	
//political interest
mvdecode w1sce4 w2sce4, mv(8)
gen polint = w1sce4 if wave == 1
replace polint = w2sce4 if wave == 2
rev polint, replace	
lab var polint "Political Interest"

//economic hardship
alpha w1scd7a w1scd7b w1scd7c w1scd7d w1scd7e , item //0,7019
alpha w2scd7a w2scd7b w2scd7c w2scd7d w2scd7e, item //0,748

factor w1scd7a w1scd7b w1scd7c w1scd7d w1scd7e
factor w2scd7a w2scd7b w2scd7c w2scd7d w2scd7e

//preparing the variables
gen econhard_w1 = 1 if w1scd7a == 1 | w1scd7b == 1 | w1scd7c == 1  | w1scd7d == 1 |  w1scd7e == 1
egen w1finanzmiss = rowmiss(w1scd7a w1scd7b w1scd7c w1scd7d w1scd7e) 
replace econhard_w1 = 0 if econhard_w1 ==. & w1finanzmiss != 5
fre econhard_w1 if wave == 1	//mising: 0,44%

tab econhard_w1 wave, m

gen econhard_w2 = 1 if w2scd7a == 1 | w2scd7b == 1 | w2scd7c == 1 | w2scd7d == 1 |   w2scd7e == 1
egen w2finanzmiss = rowmiss(w2scd7a w2scd7b w2scd7c w2scd7d w2scd7e) 
replace econhard_w2 = 0 if econhard_w2 ==. & w2finanzmiss != 5
fre econhard_w2 if wave == 2	//mising: 1,02%	

tab econhard_w2 wave, m

gen econhard = .
replace econhard =  econhard_w1  if wave == 1
replace econhard =  econhard_w2 if wave == 2
fre econhard //missing: 0,73% --> 30 cases
drop if econhard == . 

tab econhard wave	

//creating metric variable for aditional analyses
recode w1scd7a w1scd7b w1scd7c w1scd7d w1scd7e (2 = .)	
recode w2scd7a w2scd7b w2scd7c w2scd7d w2scd7e (2 = .)	

egen index_econ_w1 = rowtotal(w1scd7a w1scd7b w1scd7c w1scd7d w1scd7e)
egen index_econ_w2 = rowtotal(w2scd7a w2scd7b w2scd7c w2scd7d w2scd7e)

gen index_econ = 	index_econ_w1 if wave == 1
replace index_econ = index_econ_w2 if wave == 2
	
lab define econhard 1 "Yes"	0 "No"
lab val econhard econhard	
	
	
**# Preparing control variables
*******************************************************************

//gender
gen female = . 
replace female =  1  if w1csex == 2 & female == . 
replace female =  1  if w2csex == 2 & female == . 
replace female = 0 if female != 1

//education
gen inschool1=1
	replace inschool1=0 if w1fa22==1 | w1fa22==2

gen unemp1=0
	replace unemp1=1 if w1fa35==2 & inschool1!=0
	replace unemp1=1 if w1fa58==2

gen inschool2=1
	replace inschool2=0 if w2fa22==1 | w2fa22==2

gen unemp2=0
	replace unemp2=1 if w2fa35==2 & inschool2!=0
	replace unemp2=1 if w2fa58==2
	
gen educ=.
label value educ educ
label var educ "Education"

replace educ=1 if w1fa23ac02 ==0 | w1fa23ac02 ==1
replace educ=2 if w1fa23ac03 ==1 | w1fa23ac04 == 1 | w1fa23ac05 == 1 | w1fa23ac06 == 1 | w1fa23ac07 == 1 | w1fa23ac08 == 1
replace educ=3 if w1fa23ac09 ==1 | w1fa23ac10 == 1 | w1fa23ac11 == 1 | w1fa23ac12 == 1

label define educ 1"no/poor education" 2"medium education" 3"high education"
lab val educ educ

sort id wave educ	
replace educ = educ[_n-1] if wave == 2

//unemployment	
gen unemp = unemp1 if wave == 1
replace unemp = unemp2 if wave == 2
lab var unemp "Unemployment"

//income
recode w1fa61 99=., gen(income1)
recode w2fa61 17=., gen(income2)

gen income = income1
replace income = income2 if income == .

//satisfaction with social life
gen satsoc = w1sca4g if wave == 1
replace satsoc = w2sca4g if wave == 2
lab var satsoc "Social life satisfaction"	
mvdecode satsoc, mv(11=.)	
				
//age
gen age = . 
replace age = w1cage if age == . 
replace age = w2cage if age == . 
lab var age "age"


**# Preparing contextual variables
*******************************************************************		

//Income	
gen m_inc = w1cmunici2009INK_INW2 if wave == 1
replace m_inc = w2cmunici2012INK_INW if wave == 2
lab var m_inc "Kontext: average income before tax, per person (wages, transfers, other; *1000euro)"

//Non-Western Ethnic Origin		
gen m_nonwest =  w1cmunici2009P_N_W_AL	 if wave == 1
replace m_nonwest = w2cmunici2012P_N_W_AL if wave == 2
lab var m_nonwest "Kontext Municipalities: % Non-Western ethnic origin"

//social expenditure
egen socexp = rowtotal(m_infrastructure m_economy m_education m_sport m_social m_health m_housing)
replace socexp = socexp / 1000
	
factor m_infrastructure m_economy m_education m_sport m_social m_health m_housing // check factor loadings


**# Creating further variables 
*******************************************************************		

//years
gen year = . 
replace year = 2009 if wave == 1
replace year = 2013 if wave == 2

//Regional identifier
rename GM_NAAM region
encode region, gen(reg_id)

//District identifier
gen district_id = w1cdistrict2009id if wave == 1
replace district_id = w2cdistrict2012id if wave == 2
order district_id, first
label var district_id "District_ID"

xtset id  year

rename id pers_id
order district_id reg_id, first

egen reg_year = group(reg_id year)

//creating sample of immigrants
gen migrants_sample = . 
replace migrants_sample = 1 if w1cethnic != 9
replace migrants_sample = 1 if w2cethnic != 9

replace migrants_sample = 0 if w1cethnic == 9
replace migrants_sample = 0 if w2cethnic == 9
		
//balancing panel
bys pers_id: gen nwave = [_N]  
drop if nwave != 2		

//Excluding districts with very low number of observations & wrong data 
drop if reg_id == 32 | reg_id == 14
drop if reg_id == 13
replace socexp = 4.366 if reg_id == 26
xtset pers_id year	

//weight
gen weight = . 
sort pers_id wave 
replace weight = w1cweight1 if w1cweight1 != .
replace weight = w1cweight1[_n-1] if (weight == .) & (pers_id == pers_id[_n-1])		
lab var weight "weight"


**# Creating transitions of economic hardship variables
*******************************************************************		
				
sort pers_id year 
replace econhard_w1 = econhard_w1[_n-1] if (econhard_w1 == .) & (pers_id == pers_id[_n-1])
replace econhard_w2 = econhard_w2[_n+1] if (econhard_w2 == .) & (pers_id == pers_id[_n+1])

gen poorrich = 0
replace poorrich = econhard_w1 == 1 & econhard_w2 == 0

gen poorpoor = 0
replace poorpoor = econhard_w1 == 1 & econhard_w2 == 1

gen richpoor = 0
replace richpoor = econhard_w1 == 0 & econhard_w2 == 1

gen richrich = 0
replace richrich = econhard_w1 == 0 & econhard_w2 == 0 

gen econcat = .
replace econcat = 1 if poorrich == 1
replace econcat = 2 if poorpoor == 1
replace econcat = 3 if richpoor == 1
replace econcat = 4 if richrich == 1			

lab define econcat 1 "poorrich" 2 "poorpoor" 3 "richpoor" 4 "richrich"
lab val econcat econcat

save "nells_panel_long_cleaned_context.dta", replace
	
	
**# Sample & Splitsample
*******************************************************************		
					
mixed distrust2 i.econhard c.socexp i.migrants_sample age female satsoc polint  i.educ unemp income ///
m_inc m_nonwest m_sted i.year   ||reg_id: econhard 	||pers_id:   	,stddeviations	
keep if e(sample)

//balancing the panel
sort pers_id year
bys pers_id: gen bla = _N
drop if bla == 1

//standardize the variables
cap drop z_* 
foreach var of varlist dissatis index_econ distrust2 socexp dissatis_w2 age satsoc polint income m_inc m_nonwest m_sted  hh_socmin{
	egen z_`var' = std(`var')
}	

//creating split sample
preserve
	keep reg_id  socexp  year
	sort reg_id  socexp
	collapse (mean) socexp if year == 2013  , by(reg_id)
		
	sum socexp, detail
	assert socexp != .
	gen high_socexp = 1 if socexp >= `r(p50)' & socexp != .
		replace high_socexp = 0 if high_socexp != 1 & socexp != .				
					
	tempfile high_soz
	save `high_soz'
restore

merge m:1 reg_id using  `high_soz'	
drop _merge

	
*********************
**# Results in section "Results"
*********************

table reg_id, stat(mean dissatis) totals()	//min and max per region

table year, stat(mean dissatis)
	
	
*********************
**# Fig 1 - Distribution of political disatisfaction
*********************	

preserve
	use "nells_panel_long_cleaned_context.dta", clear

	sort pers_id year
	bys pers_id: gen bla = _N
	bys pers_id: gen bla2 = _n

	drop if bla == 1
	
	keep year dissatis distrust2 reg_id econhard
	collapse dissatis distrust2 , by(reg_id econhard year)
	bys econhard: sum dissatis distrust2
	sort reg_id
	
	gen sortierung = dissatis if econhard == 1
	gen sortierung2 = distrust2 if econhard == 1
	
	sum dissatis
	graph bar dissatis , over(econhard) over(reg_id, sort(sortierung) label(angle(45))) scheme(plotplain) ///
		intens(80) ylab(0(1)5) ///
		bargap(0) legend(pos(3)) ///
		title() ytitle(Political Dissatisfaction) ///
		note("Source: NELLS wave 1 and 2. Weights applied. The blue line indicates the average of all municipalities over the two waves for individuals with" "economic hardship (m = 3.53), the red line for individuals without economic hardship (m = 3.07).", span size(vsmall)) yline(3.53, lpattern(dashed) lcolor(  navy   )) yline(3.07, lpattern(dashed) lcolor(maroon))  asyvars ///
		legend(pos(6) rows(1) label(1 "without economic hardship") label(2 "with economic hardship")) bar(1, bcolor(maroon) fintensity(*0.9)) bar(2, bcolor( navy ) fintensity(*0.8) )  		
				
	graph export  "HV_Dissatisfaction_Fig1.png"	, replace							
restore	
	

*********************
**# Fig 2 - Predicted values of political dissatisfaction
*********************	
		
mixed dissatis i.econhard##c.socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin i.year   ||reg_id:  	||pers_id:  [pweight=weight] 	,stddeviations	vce(cluster reg_id) 
margins, at(socexp =(0(1)6)  econhard=(0 1))  vsquish  
marginsplot,  recast(line) ciopts(recast(rcap) color(%40))  ylabel(2.8(.2)3.6) ///
	ytitle(Predicted Values of political dissatisfaction (1-5), size(small)) title("") ///
	xtitle("Local social spending in 1,000€ per capita", size(small)) legend(order(2 "economic hardship" 1 "no economic hardship") on pos(6)  row(1)) ///
	note("Source: NELLS Wave 1 and 2, contextual data obtained from Findo.NL, based on Models M4. Weights applied." ,span size(vsmall))

graph export  "HV_Dissatisfaction_Fig2.png"	, replace		

		  
*********************
**# Tab 1 - Regression models using political dissatisfaction
*********************

mixed z_dissatis  i.year   ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 	vce(cluster reg_id) 
estat icc
est sto m0

mixed z_dissatis i.econhard c.z_socexp  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year   ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 	vce(cluster reg_id) 
est sto m1

mixed z_dissatis i.econhard  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  if high_socexp == 0  ||reg_id:  	||pers_id: [pweight=weight]  	,stddeviations		vce(cluster reg_id) 
est sto m2

mixed z_dissatis i.econhard  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  if high_socexp == 1 ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 vce(cluster reg_id) 
est sto m3

mixed z_dissatis i.econhard##c.z_socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin i.year   ||reg_id:  	||pers_id:  [pweight=weight] 	,stddeviations	vce(cluster reg_id) 
est sto m4

esttab m0 m1 m2 m3 m4

esttab m0 m1 m2 m3 m4 using "HV_Dissatisfaction_Tab1.rtf" ,  b(%8.3f)  replace nogap se	///
	order(*.econhard female z_age unemp z_income educ z_satsoc migrants_sample z_polint  m_sted z_m_inc z_m_nonwest z_socexp year)  ///
	onecell transform(ln*: exp(2*@) 2*exp(2*@)) star() stats(aic N)		
	
	
					
	
*********************
**# Tab 2 - Regression models using transition of economic hardship
*********************

preserve
	keep if year == 2013 //keep second wave
	
	//standardize variables with new sample
	cap drop z_*
	foreach var of varlist socexp dissatis_w2 age satsoc polint income m_inc m_nonwest m_sted hh_socmin{
		egen z_`var' = std(`var')
	}			
	
	//Models
	mixed z_dissatis_w2   b4.econcat [pweight=weight] ||reg_id: , stddeviations vce(cluster reg_id) 
		est sto m5
		estat icc
		
	mixed z_dissatis_w2 b4.econcat i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted z_socexp z_hh_socmin [pweight=weight] ||reg_id: ,stddeviations vce(cluster reg_id) 
		est sto m6
				
	mixed z_dissatis_w2 b4.econcat i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted z_hh_socmin if high_socexp == 0 [pweight=weight] ||reg_id: ,stddeviations vce(cluster reg_id) 
		est sto m7
		
	mixed z_dissatis_w2 b4.econcat i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted  z_hh_socmin if high_socexp == 1 [pweight=weight] ||reg_id: ,stddeviations vce(cluster reg_id) 
		est sto m8
		
	mixed z_dissatis_w2 b4.econcat##c.z_socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted z_hh_socmin [pweight=weight] ||reg_id: ,stddeviations vce(cluster reg_id) 
		est sto m9
		
	esttab m5 m6 m7 m8 m9
	esttab m5 m6 m7 m8 m9 using "HV_Dissatisfaction_Tab2.rtf" ,  b(%8.3f)  replace nogap se	///
		onecell transform(ln*: exp(2*@) 2*exp(2*@)) star() drop() stat(aic N) 
restore
	
	
*********************
**# Tab A1 - Sample Descriptives
*********************	

cap drop econcat_* educ_*
tab econcat, gen(econcat_)
tab educ, gen(educ_)
asdoc tabstat dissatis econhard econcat_* migrants_sample age female satsoc polint educ_* unemp income  ///
	  m_inc m_nonwest m_sted socexp hh_socmin [fweight=weight], statistics(mean sd min max) fs(8) replace dec(2) save(HV_Dissatisfaction_Appendix_A1) 		
	  
*********************
**# Tab A2 - Correlation Matrix 
*********************	

asdoc cor dissatis econhard migrants_sample age female satsoc polint educ unemp income  ///
	  m_inc m_nonwest m_sted socexp hh_socmin [fweight=weight] , fs(8) replace dec(2) save(HV_Dissatisfaction_Appendix_A2) 
		  
*********************
**# Fig A3 & A4 - social expenditure per municipality
*********************

preserve
	use "nells_panel_long_cleaned_context.dta", clear

	sort pers_id year
	bys pers_id: gen bla = _N
	bys pers_id: gen bla2 = _n

	drop if bla == 1
	drop if reg_id == 32 | reg_id == 14
	
	keep year dissatis socexp reg_id  weight
	collapse dissatis socexp weight , by(reg_id year)
	sort reg_id
	
	gen sortierung = dissatis 
	gen sortierung2 = socexp 
	
	replace socexp = socexp * 1000

	//A3
	sum dissatis
	graph bar dissatis [aweight=weight], over(reg_id, sort(dissatis) label(angle(45))) scheme(plotplain) ///
		intens(80) ylab(-1.0(0.25)1)  ylabel(0(1)5) ///
		ytitle ("Political dissatisfaction (1-5)", size(small)) title() ///
		note("Source: NELLS wave 1 and 2, contextual data obtained from Findo.NL, weights applied." ,span size(vsmall))
	graph export  "HV_Dissatisfaction_Appendix_A3.png" , replace
	
	//A4
	sum socexp 
	graph bar socexp [aweight=weight], over(reg_id, sort(socexp ) label(angle(45))) scheme(plotplain) ///
		intens(80) ylab(-1.0(0.25)1)  ylabel(0(1000)5500) ///
		ytitle ("Local social spending in Euro per capita", size(small)) title() ///
		note("Source: NELLS wave 1 and 2, contextual data obtained from Findo.NL, weights applied." ,span size(vsmall))
	graph export  "HV_Dissatisfaction_Appendix_A4.png" , replace
	
restore	
		
		
*********************
**# Fig A5 - Regression models using metric economic hardship
*********************		
		
mixed z_dissatis c.z_index_econ c.z_socexp  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year   ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 	vce(cluster reg_id) 
	est sto r1

mixed z_dissatis c.z_index_econ  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  if high_socexp == 0  ||reg_id:  	||pers_id: [pweight=weight]  	,stddeviations		vce(cluster reg_id) 
	est sto r2
		
mixed z_dissatis c.z_index_econ  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  if high_socexp == 1 ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 vce(cluster reg_id) 
	est sto r3
		
mixed z_dissatis c.z_index_econ##c.z_socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin i.year   ||reg_id:  	||pers_id:  [pweight=weight] 	,stddeviations	vce(cluster reg_id) 
	est sto r4
			
esttab r1 r2 r3  r4

esttab r1 r2 r3  r4 using "HV_Dissatisfaction_Appendix_A5.rtf" ,  b(%8.3f)  replace nogap se	///
	order(*.z_index_econ* female z_age unemp z_income educ z_satsoc migrants_sample z_polint  m_sted z_m_inc z_m_nonwest z_socexp year)  ///
	onecell transform(ln*: exp(2*@) 2*exp(2*@)) star() stats(aic N)		
	
	
*********************
**# Fig A6 - Margins 
*********************		

mixed dissatis c.index_econ##c.socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin i.year   ||reg_id:  	||pers_id:  [pweight=weight] 	,stddeviations	vce(cluster reg_id) 
margins, at(socexp =(0(1)6)  index_econ=(0  1 5))  vsquish  
	marginsplot,  recast(line) ciopts(recast(rcap) color(%40))  ylabel(3(.5)4.5) ///
		ytitle("Predicted Values of Political Dissatisfaction (1-5)", size(small)) title("") ///
		xtitle("Local social spending in 1,000€ per capita", size(small)) legend(order(1 "no economic problems" 2 "1 economic problem" 3 "5 economic problems") on pos(6)  row(1)) ///
		note("Source: NELLS Wave 1 and 2, contextual data obtained from Findo.NL, based on Models R4 of Appendix A4. Weights applied." ,span size(vsmall))

graph export  "HV_Dissatisfaction_Appendix_A6.png"	, replace							
	

*********************
**# Fig A7 - Transitions of economic hardship
*********************	

	xttrans econhard, freq
		

*********************
**# Fig A8 - Regression models using alternative measure for social expenditure
*********************		

//creating alternative social expenditure measure (for A6)
gen socexp_v2_2009 =  P_WW_UIT_2009 + P_WW_UIT_2012
gen socexp_v2_2012 =  P_WWB_UIT_2009 + P_WWB_UIT_2012
	
gen  socexp_v2 = socexp_v2_2009 if wave == 1
	replace socexp_v2 = socexp_v2_2012 if wave == 2
		  
egen z_socexp_v2 = std(socexp_v2)		  

//Creating splitsample with alternativ measure
preserve
	keep reg_id  socexp_v2   year
	sort reg_id  socexp_v2 
	collapse (mean) socexp_v2 if year == 2013  , by(reg_id)
		
	sum socexp_v2, detail
	gen high_socexp_v2 = 1 if socexp_v2 >= `r(p50)' & socexp_v2 != .
		replace high_socexp_v2 = 0 if high_socexp_v2 != 1 & socexp_v2 != .						

	tempfile high_soz
	save `high_soz'
restore

merge m:1 reg_id using  `high_soz'	
drop _merge


//Regression models
mixed z_dissatis i.econhard c.z_socexp_v2  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year   ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 	vce(cluster reg_id) 
	est sto d1
		
mixed z_dissatis i.econhard  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  if high_socexp_v2 == 0  ||reg_id:  	||pers_id: [pweight=weight]  	,stddeviations		vce(cluster reg_id) 
	est sto d2		
		
mixed z_dissatis i.econhard  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  if high_socexp_v2 == 1 ||reg_id:  	||pers_id:   [pweight=weight]	,stddeviations	 vce(cluster reg_id) 
	est sto d3 
	
mixed z_dissatis i.econhard##c.z_socexp_v2 i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin i.year   ||reg_id:  	||pers_id:  [pweight=weight] 	,stddeviations	vce(cluster reg_id) 
	est sto d4
			
esttab d1 d2 d3 d4

esttab d1 d2 d3 d4 using "HV_Dissatisfaction_Appendix_A8.rtf" ,  b(%8.3f)  replace nogap se	///
	order(*.econhard* female z_age unemp z_income educ z_satsoc migrants_sample z_polint  m_sted z_m_inc z_m_nonwest z_socexp year)  ///
	onecell transform(ln*: exp(2*@) 2*exp(2*@)) star() stats(aic N)		

	
*********************
**# Fig A9 - OLS Regression of main results of table 1 and table 2
*********************		

reg z_dissatis i.econhard c.z_socexp  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year i.reg_id   [pweight=weight]	,	 	vce(cluster reg_id) 
est sto r1

reg z_dissatis i.econhard  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin   i.year  i.reg_id if high_socexp == 0    [pweight=weight]  	,		vce(cluster reg_id) 
est sto r2

reg z_dissatis i.econhard  i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin    i.year i.reg_id  if high_socexp == 1    [pweight=weight]	,	 vce(cluster reg_id) 
est sto r3

reg z_dissatis i.econhard##c.z_socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income ///
	z_m_inc z_m_nonwest m_sted z_hh_socmin i.year i.reg_id    [pweight=weight] 	,	vce(cluster reg_id) 
est sto r4

	esttab r1 r2 r3 r4 
	esttab r1 r2 r3 r4 using "HV_Dissatisfaction_Appendix_A9_part1.rtf" ,  b(%8.3f)  replace nogap se	///
		onecell transform(ln*: exp(2*@) 2*exp(2*@)) star() drop() stat(aic N) 	

	
preserve
	keep if year == 2013 
	
	//standardize variables with new sample
	cap drop z_*
	foreach var of varlist socexp dissatis_w2 age satsoc polint income m_inc m_nonwest m_sted hh_socmin{
		egen z_`var' = std(`var')
	}			
	
	//Models
	reg z_dissatis_w2   b4.econcat  i.reg_id  [pweight=weight],  vce(cluster reg_id) 
		est sto r5
		
	reg z_dissatis_w2 b4.econcat i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted z_socexp z_hh_socmin i.reg_id  [pweight=weight] , vce(cluster reg_id) 
		est sto r6
				
	reg z_dissatis_w2 b4.econcat i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted z_hh_socmin i.reg_id  if high_socexp == 0 [pweight=weight] , vce(cluster reg_id) 
		est sto r7
		
	reg z_dissatis_w2 b4.econcat i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted  z_hh_socmin  i.reg_id if high_socexp == 1 [pweight=weight] , vce(cluster reg_id) 
		est sto r8
		
	reg z_dissatis_w2 b4.econcat##c.z_socexp i.migrants_sample z_age female z_satsoc z_polint  i.educ unemp z_income z_m_inc z_m_nonwest m_sted z_hh_socmin i.reg_id [pweight=weight]   , vce(cluster reg_id) 
		est sto r9
		
	esttab r5 r6 r7 r8 r9
	esttab r5 r6 r7 r8 r9 using "HV_Dissatisfaction_Appendix_A9_part2.rtf" ,  b(%8.3f)  replace nogap se	///
		onecell transform(ln*: exp(2*@) 2*exp(2*@)) star() drop() stat(aic N) 
restore
	

	
	
	ex






